package com.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.enums.isManagerEnum;
import com.forum.mapper.ForumArticleMapper;
import com.forum.pojo.ForumArticle;
import com.forum.pojo.StudentInfo;
import com.forum.pojo.vo.ArticleModify;
import com.forum.pojo.vo.SearchStudentVo;
import com.forum.pojo.vo.StudentVo;
import com.forum.result.Result;
import com.forum.result.ResultCodeEnum;
import com.forum.service.StudentInfoService;
import com.forum.mapper.StudentInfoMapper;
import com.forum.utils.JwtHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo>
    implements StudentInfoService{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StudentInfoMapper studentInfoMapper;
    @Autowired
    private ForumArticleMapper forumArticleMapper;
    @Override
    public Result verify(String username, String password) {
        QueryWrapper<StudentInfo> studentInfoQueryWrapper = new QueryWrapper<>();
        studentInfoQueryWrapper.eq("studentId",username);
        if(studentInfoMapper.selectOne(studentInfoQueryWrapper)==null){
            return  Result.build(null, ResultCodeEnum.USENAME);
        }
        studentInfoQueryWrapper.eq("password",password);
        if(studentInfoMapper.selectOne(studentInfoQueryWrapper)==null){
            return  Result.build(null,ResultCodeEnum.PASSWORD);
        }
        StudentInfo studentInfo = studentInfoMapper.selectOne(studentInfoQueryWrapper);
        String key="token:"+UUID.randomUUID().toString();
        String student = JwtHelper.createToken(studentInfo.getStudentid(), "student");
        redisTemplate.opsForValue().set(key,student,48, TimeUnit.HOURS);
        System.out.println(key);
        return  Result.ok(key);
    }

    @Override
    public Result getPersonInfo(Long userId) {
        QueryWrapper<StudentInfo> studentInfoQueryWrapper = new QueryWrapper<>();
        studentInfoQueryWrapper.eq("studentId",userId);
        return Result.ok(studentInfoMapper.selectList(studentInfoQueryWrapper));
    }

    @Override
    public Result<Page> getStudentsBySelector(SearchStudentVo searchStudentVo, Long currentPage, Long limit) {
        QueryWrapper<StudentInfo> queryWrapper=new QueryWrapper<>();
        String grade = searchStudentVo.getGrade();
        String major = searchStudentVo.getMajor();
        if(StringUtils.isNotEmpty(grade)){
            queryWrapper.like("grade",grade);
        }
        if(StringUtils.isNotEmpty(major)){
            queryWrapper.like("major",major);
        }
        Page page=new Page(currentPage,limit);
        return Result.ok(studentInfoMapper.selectPage(page,queryWrapper));
    }

    @Override
    public int updateIsAdmin(Long studentId, Integer isAdmin) {
        StudentInfo studentInfo = studentInfoMapper.selectById(studentId);
        studentInfo.setIsadmin(isAdmin);
        return studentInfoMapper.updateById(studentInfo);
    }

    @Override
    public int updateIsForbidden(Long studentId, Integer isForbidden) {
        StudentInfo studentInfo = studentInfoMapper.selectById(studentId);
        studentInfo.setStates(isForbidden);
        return studentInfoMapper.updateById(studentInfo);
    }

    @Override
    public int insertStudent(StudentVo studentVo) {
        StudentInfo studentInfo=new StudentInfo();
        Long studentid = studentVo.getStudentid();
        String id=String.valueOf(studentid);
        String password="zjut"+id.substring(id.length()-6);
        studentInfo.setStudentid(studentVo.getStudentid());
        studentInfo.setPassword(password);
        studentInfo.setMajor(studentVo.getMajor());
        studentVo.setGrade(studentVo.getGrade());
        return studentInfoMapper.insert(studentInfo);
    }

    @Override
    public void lessCount(Long informedUserId) {
        StudentInfo studentInfo = studentInfoMapper.selectById(informedUserId);
        studentInfo.setIllegalcount(studentInfo.getIllegalcount()-1);
        studentInfoMapper.updateById(studentInfo);
    }

    @Override
    public List<ArticleModify> getPersonArticleById(Long userId) {
        QueryWrapper<ForumArticle> studentInfoQueryWrapper = new QueryWrapper<>();
        studentInfoQueryWrapper.eq("user_id",userId);
        List<ForumArticle> forumArticles = forumArticleMapper.selectList(studentInfoQueryWrapper);
        List<ArticleModify> articleModifies = new ArrayList<>();
        for(ForumArticle item:forumArticles){
            ArticleModify articleModify = new ArticleModify();
            articleModify.setForumArticle(item);
            Long Id = item.getUserId();
            Integer integer = forumArticleMapper.selectIdentity(Id);
            articleModify.setIdentity(integer);
            articleModify.setAvatorUrl(forumArticleMapper.findAvartarUrlByUserId(Id));
            articleModifies.add(articleModify);
        }
        return articleModifies;
    }
}




